package org.baojie.raft.message;

import java.util.concurrent.ThreadLocalRandom;

public class MsgID {

    private final ThreadLocalRandom random = ThreadLocalRandom.current();

    private static final String array = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static volatile MsgID instance;

    public static final MsgID getInstance() {
        if (null == instance) {
            synchronized (MsgID.class) {
                if (null == instance) {
                    instance = new MsgID();
                }
            }
        }
        return instance;
    }

    public final String msgId() {
        StringBuilder builder = new StringBuilder(64);
        long now = System.nanoTime();
        builder.append(now);
        builder.append("-");
        for (int i = 0; i < 6; i++) {
            int index = random.nextInt(62);
            char c = array.charAt(index);
            builder.append(c);
        }
        String id = builder.toString();
        return id;
    }

}
